package br.edu.granbery.indianostore.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.edu.granbery.indianostore.util.HibernateUtil;

public class DaoFactory {

	private final Session session;
	private Transaction transaction;

	public DaoFactory() {
		session = HibernateUtil.getSession();
	}

	public void beginTransaction() {

		if (hasTransaction()) {
			stopTransaction();
		}
		this.transaction = this.session.beginTransaction();
	}

	public void commit() {
		this.transaction.commit();
		this.transaction = null;
	}

	public boolean hasTransaction() {
		return this.transaction != null;
	}

	public void rollback() {
		this.transaction.rollback();
		this.transaction = null;
	}

	public void stopTransaction() {
		this.transaction = null;
	}

	public void close() {
		this.session.close();
	}

	public void clear() {
		this.session.clear();
	}

	public CarrinhoDao getCarrinhoDao() {
		return new CarrinhoDao(this.session);
	}
	public ClienteDao getClienteDao() {
		return new ClienteDao(this.session);
	}
	public ItemCarrinhoDao getItemCarrinhoDao() {
		return new ItemCarrinhoDao(this.session);
	}
	public ProdutoDao getProdutoDao() {
		return new ProdutoDao(this.session);
	}
	public UsuarioDao getUsuarioDao() {
		return new UsuarioDao(this.session);
	}
	public VendaDao getVendaDao() {
		return new VendaDao(this.session);
	}

}

